<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加项目文件</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>
<body>
<form class="layui-form" action="" lay-filter="editform">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required|len" autocomplete="off" min="3" placeholder="请输入名称" class="layui-input">
                </div>
            </div>
			<div class="layui-form-item">
			    <label class="layui-form-label">类别</label>
			    <div class="layui-input-block">
			        <select name="type" lay-search id="key-area">
			        </select>
			    </div>
			</div>
			<div class="layui-form-item">
			    <label class="layui-form-label">编号</label>
			    <div class="layui-input-block">
			        <input type="text" name="file_no" autocomplete="off" min="3" placeholder="请输入名称" class="layui-input">
			    </div>
			</div>
			<div class="layui-form-item">
			    <label class="layui-form-label">型号</label>
			    <div class="layui-input-block">
			        <input type="text" name="version" autocomplete="off" min="3" placeholder="请输入名称" class="layui-input">
			    </div>
			</div>
			<div class="layui-form-item">
			    <label class="layui-form-label">缩略图</label>
			    
			     <div class="layui-input-inline">
			        <input type="text" name="img" value="" autocomplete="off" min="2" id="logo" placeholder="png/jpg/jpeg/gif格式" class="layui-input" readonly>
			     </div>
			     <div class="layui-input-inline">
			        <button type="button" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" id="uploadBtn">
			        <i class="layui-icon">&#xe67c;</i>上传
			        </button>
			    </div>
			</div>
			
			<div class="layui-form-item">
			    <label class="layui-form-label">文件</label>
			    
			     <div class="layui-input-inline">
			        <input type="text" name="path" value="" autocomplete="off" min="2" id="path" placeholder="*格式" class="layui-input" readonly>
			     </div>
			     <div class="layui-input-inline">
			        <button type="button" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" id="uploadPath">
			        <i class="layui-icon">&#xe67c;</i>上传
			        </button>
			    </div>
			</div>
			
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">应用场景</label>
				<div class="layui-input-block">
				<textarea name="desc" placeholder="请输入应用场景" class="layui-textarea"></textarea>
				</div>
			</div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
			<input  type="hidden" name="id"/>
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="profile-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../config.js"></script>
<script src="/static/layui/layui.js"></script>
<script src="/static/ui/ui.js"></script>
<script>
    layui.use(['form','layer','tree','upload'], function () {
        let form = layui.form
            , layer = layui.layer
			, $ = layui.$
			, upload = layui.upload
            , req = _req();
        form.verify({
            len : (val, item) => {
                let min = item.getAttribute("min")
                if(val.length < min) {
                    return '长度不能小于' + min;
                }
            }
        })
		let uploadInst = upload.render({
		    elem: '#uploadBtn' //绑定元素
		    ,url: apiUrl + 'upload/index' //上传接口
		    ,headers : getHeader()
		    ,exts : 'jpg|png|gif|bmp|jpeg'
		    ,field : 'file'
		    ,acceptMime: 'image/*'
			//,accept : 'images'
		    ,done: function(res){
		    //上传完毕回调
		        console.log(res)
		        $('#logo').val(res.data.path)
		    }
		    ,error: function(){
		    //请求异常回调
		        
		    }
		});
		
		upload.render({
		    elem: '#uploadPath' //绑定元素
		    ,url: apiUrl + 'upload/index' //上传接口
		    ,headers : getHeader()
		    //,exts : 'jpg|png|gif|bmp|jpeg'
		    ,field : 'file'
		    //,acceptMime: 'image/*'
			,accept : 'file'
		    ,done: function(res){
		    //上传完毕回调
		        console.log(res)
		        $('#path').val(res.data.path)
		    }
		    ,error: function(){
		    //请求异常回调
		        
		    }
		});
		const renderSelect = (rs) => {
			// for(let p in rs) {
			// 	$("#key-area").append(new Option(rs[p], p));
			// }
			rs.forEach(el => {
				$("#key-area").append(new Option(el.name, el.id));
			})
			// res.tableList
			form.render("select");
		}
        if(req.id) {
            //渲染
            _get(layui, 'profile/editBefore?id=' + req.id, res => {
				renderSelect(res.cates)
                form.val('editform', res.data)
            });
        }else{
			_get(layui, 'profile/addBefore', res => {
				//console.log(res)
			    renderSelect(res)
			});
		}

        //监听提交
        form.on('submit(profile-save)', function (data) {
			data = data.field;
            let postUrl;
            if(req.id) {
                postUrl = 'profile/edit';
            }else{
                postUrl = 'profile/add';
            }
			_post(layui, postUrl, data, res => {
				//console.log(res)
				parent.layui.table.reload("profile-table");
				parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页

			})

            return false;
        });

    });
</script>

</body>
</html>